package com.tdam2014.grupo02;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import com.tdam2014.grupo02.DataBase.DBHelper;

import android.content.Context;

public class Comment {

private String commentName;
private String commentMessage;
private String commentDate;
private String commentFlickrId;
private int commentPictureId;
private static int dbVersion = 4;

public Comment (String name, String message, String date, int photoId)
{
	this.commentMessage = message;
	this.commentName = name;
	this.setCommentDate(date);
	this.commentPictureId = photoId;
	
}

public Comment (JSONObject jsonObj)
{
	this.commentMessage = jsonObj.optString("_content");
	this.commentName = jsonObj.optString("authorname");	
	
}

	public static ArrayList<Comment> generateComment(int photo) 
	{
		ArrayList<Comment> list = new ArrayList<Comment>();
		for (int i = 0; i < 4; i++) {
			list.add(new Comment("Usuario " + i, "Mensaje Usuario " + i, new Date(System.currentTimeMillis()).toString(), photo));
		}
		return list;
	}
	public static ArrayList<Comment> generateComments(int pictureId, Context context) 
	{
		DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
		ArrayList<Comment> list = new ArrayList<Comment>();
		for (int i = 0; i < 4; i++) 
		{
			Comment com = new Comment("Usuario " + i, "Mensaje Usuario " + i, new Date(System.currentTimeMillis()).toString(), pictureId);
			dbHelper.newComment(com, context);
		}
		return list;
	}
	public String getCommentName() {
		return commentName;
	}
	public void setCommentName(String commentName) {
		this.commentName = commentName;
	}
	public String getCommentMessage() {
		return commentMessage;
	}
	public void setCommentMessage(String commentDescription) {
		this.commentMessage = commentDescription;
	}
	public String getCommentDate() {
		return commentDate;
	}
	public void setCommentDate(String commentDate) {
		this.commentDate = commentDate;
	}
	public int getCommentPhotoId() {
		return commentPictureId;
	}
	public void setCommentPhotoId(int commentPictureId) {
		this.commentPictureId = commentPictureId;
	}

	public static ArrayList<Comment> getComments(Context context, int pictureId)
    {
        DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
    		
   	    return dbHelper.getComments(context, pictureId);
    }
	public String getCommentFlickrId() {
		return commentFlickrId;
	}
	public void setCommentFlickrId(String commentFlickrId) {
		this.commentFlickrId = commentFlickrId;
	}
	
	public static void updatePicsComments (Context context, Picture pic) throws ClientProtocolException, JSONException, IOException
    {
    	DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
    	ArrayList<Comment> comments = new ArrayList<Comment>();
    	FlickrConnect.getPictureComments(pic.getPictureFlickrId());
       	dbHelper.updatePictureComments(comments, context);   	
    }
	
}
